#include "stdafx.h"
#include "Model.h"
#include "ShaderTypes.h"
#include "Renderer.h"
#include <assimp/Importer.hpp>      // C++ importer interface
#include <assimp/scene.h>           // Output data structure
#include <assimp/postprocess.h>     // Post processing flags

using namespace DirectX;
using namespace Rendering;

Model::Model(Renderer* renderer, std::string vertexShaderFile, std::string pixelShaderFile)
{
	Assimp::Importer importer;
	const aiScene* scene = importer.ReadFile("test.x", 
		aiProcess_MakeLeftHanded
		| aiProcess_CalcTangentSpace
		| aiProcess_Triangulate
		| aiProcess_JoinIdenticalVertices
		| aiProcess_SortByPType);

	if (!scene->HasMeshes())
		return;
	const aiMesh* mesh = scene->mMeshes[0];
	for(unsigned int i = 0; i < mesh->mNumVertices; ++i)
	{
		auto vertex = mesh->mVertices[i];
		m_vertices.push_back(PosColorVertex(XMFLOAT3(vertex.x, vertex.y, vertex.z), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f)));
	}

	std::vector<unsigned short> indices;
	for(unsigned int i = 0; i < mesh->mNumFaces; ++i)
	{
		aiFace face = mesh->mFaces[i];
		for(unsigned int j = 0; j < face.mNumIndices; ++j)
			indices.push_back(face.mIndices[j]);
	}

	this->SetShader(new Shader(renderer, vertexShaderFile, pixelShaderFile, PosColorVertexLayout, ARRAYSIZE(PosColorVertexLayout)));
	this->SetMesh(new GenericMesh<PosColorVertex>(renderer->Device(), static_cast<UINT>(m_vertices.size()), m_vertices.data(), static_cast<UINT>(indices.size()), indices.data()));
}


Model::~Model(void)
{
}
